import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:tot_android_flutter/utils/utils_ext.dart';
import 'package:get/get.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'package:tot_android_flutter/pages/tabpage/tree/tree_controller.dart';

import '../../../base/BasePage.dart';
import '../../../common/widget/app_bar.dart';
import '../../../eneity/tree_item_entity.dart';

class TreePage extends BasePage<TreeController> {
  TreePage({super.key}) : super();

  var currentValue = 1.obs;
  final _pageController = PageController(initialPage: 1);

  @override
  AppBar? buildAppBar() {
    return commonAppBar(statusBarColor: Colors.black45);
  }

  @override
  Widget buildBody(BuildContext context) {
    return _buildTreeSideBar(context);
  }

  Widget _buildTreeSideBar(BuildContext context) {
    return Obx(() {
      // 自定义样式
      final pages = <Widget>[];
      for (int i = 0; i < controller.projectCategorys.length; i++) {
        var treeItemEntity = controller.projectCategorys[i];
        pages.add(_getPageTree(treeItemEntity));
      }
      return Row(
        children: [
          SizedBox(
            width: 110,
            child: Container(
              child: ListView.builder(
                  itemBuilder: (BuildContext context, int position) {
                return getLeft(position);
              }),
            ),
          ),
          Expanded(
            child: PageView(
              controller: _pageController,
              scrollDirection: Axis.vertical,
              physics: const NeverScrollableScrollPhysics(),
              children: pages,
            ),
          )
        ],
      );
    });
  }

  Widget getLeft(int position) {
    void setCurrentValue(int value) {
      _pageController.jumpToPage(value);
      if (currentValue.value != value) {
        currentValue.value = value;
      }
    }

    return Obx(() {
      return GestureDetector(
        child: Container(
            color:
                currentValue.value == position ? Colors.black38 : Colors.white,
            //Container下的color属性会与decoration下的border属性冲突，所以要用decoration下的color属性
            child: SizedBox(
              height: 56.w,
              child: IntrinsicHeight(
                child: Row(
                  children: [
                    Container(
                      width: 8.w,
                      color: currentValue.value == position
                          ? Colors.black38
                          : Colors.transparent,
                    ),
                    SizedBox(width: 8.w),
                    Expanded(
                        child: Text(
                      controller.projectCategorys[position].name,
                      style: TextStyle(
                        color: currentValue.value == position
                            ? Colors.white
                            : Colors.black38,
                        fontWeight: currentValue.value == position
                            ? FontWeight.w600
                            : FontWeight.w400,
                        fontSize: 16.w,
                      ),
                    )),
                    SizedBox(width: 4.w),
                  ],
                ),
              ),
            )),
        onTap: () {
          setCurrentValue(position);
        },
      );
    });
  }

  Widget _getPageTree(TreeItemEntity treeItem) {
    return Container(
        child: Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Padding(
          padding: EdgeInsets.only(left: 20.w, top: 2.w, right: 9.w),
          child: TDText(treeItem.name,
              style: TextStyle(
                fontSize: 14.w,
              )),
        ),
        SizedBox(height: 16.w),
        Expanded(
            child: ListView(
          children: [
            Wrap(
              direction: Axis.horizontal,
              //方向
              alignment: WrapAlignment.start,
              //内容排序方式
              spacing: 8.0,
              // 间距
              runSpacing: 4.0,
              // 行间距
              children: List.generate(treeItem.children.length, (int index) {
                return ActionChip(
                  //标签文字
                  label: Text(
                    treeItem.children[index].name,
                    style: const TextStyle(fontSize: 16, color: Colors.black),
                  ),
                  //点击事件
                  onPressed: () {},
                  elevation: 2,
                  backgroundColor: Colors.grey.shade200,
                );
              }),
            )
          ],
        ))
      ],
    ));
  }
}
